%% Figure 2: Impact of central clearing on the expected aggregate default loss
clear all; clc
Calibration

delta_ADL_f = @(eta_agg,alpha_CCP,alpha_uc) xi(alpha_CCP)./xi(alpha_uc).*f(1,beta)./f(K,beta).*eta_agg + f(K-1,beta)./f(K,beta)-1;
etas = 0:0.05:1;

% compute impact of central clearing for different margin level for cleared positions
alpha_CCP = 0.98;
delta_ADL_1 = delta_ADL_f(etas,alpha_CCP,alpha_uc);

alpha_CCP = 0.99;
delta_ADL_2 = delta_ADL_f(etas,alpha_CCP,alpha_uc);

alpha_CCP = 0.995;
delta_ADL_3 = delta_ADL_f(etas,alpha_CCP,alpha_uc);

% figure 
FontSize = 19;
linetypes = {'-','-',':','-.','--','--',':'};
linemarkers = {'none','o','none','.','s','d','+'};

h1 = figure();
hold on
p1=plot(etas,delta_ADL_1,'-s','Color','black','LineWidth',1.2);
p2=plot(etas,delta_ADL_2,'--red','LineWidth',1.2);
p3=plot(etas,delta_ADL_3,'-blue','LineWidth',1.2);
grid on
xlabel('Average portfolio directionality ($\eta_{agg}$)','FontSize',10,'Interpreter','latex')
ylabel('$$\Delta$$Expected default loss ($$\Delta DL$$)','FontSize',10,'Interpreter','latex')
xlim([0,1])
legende=legend([p1,p2,p3],'Small $\alpha_{CCP}$','Moderate $\alpha_{CCP}$','Large $\alpha_{CCP}$','Location','NorthWest','interpreter','latex');
set(legende,'FontSize',FontSize,'Interpreter','latex','color','none');
set(gca,'FontSize',round(FontSize*4/5));

set(h1,'PaperPositionMode','auto');
set(h1,'Position',[100 100 600 400]);

ylabels=get(gca,'YTickLabel'); % get the y axis labels
for i=1:size(ylabels,1)
    ylabels_modif{i}=sprintf('%.3g%%', str2double(ylabels{i})*100);
end
set(gca,'YTickLabel',ylabels_modif)
print(h1, '-depsc2','-loose','pics/Figure_2.eps')
